/**
 * Filename: AlbumIntroAdapter.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/3/15 18:56
 */
package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;


public class AlbumIntroAdapter extends RecyclerView.Adapter<AlbumIntroAdapter.ViewHolder> {

    private String imgUrl;
    private String introContent;
    private ImageLoader mImageLoader;
    private Activity mContext;

    public AlbumIntroAdapter(Activity mContext,String imgUrl , String   introContent) {
        this.imgUrl = imgUrl;
        this.mContext = mContext;
        this.introContent = introContent;
        mImageLoader = ImageLoader.getInstance();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_intro;
        TextView tv_intro;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_intro = (ImageView) itemView.findViewById(R.id.iv_intro);
            tv_intro = (TextView) itemView.findViewById(R.id.tv_intro);
        }

        public void bindData(String imgUrl , String   introContent) {
            mImageLoader.displayImage(imgUrl + StringUtils.getQulity(), new ImageViewAware(iv_intro , false),
                    ImageOptHelper.getBgOptions(),new SimpleImageLoadingListener() {
                        @Override
                        public void onLoadingStarted(String imageUri, View view) {
                        }
                        @Override
                        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                        }
                        @Override
                        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                            float scale = (float) loadedImage.getHeight() / loadedImage.getWidth();
                            int screenWidthPixels = DisplayUtils.getScreenWidthPixels(mContext)-DisplayUtils.dp2px(mContext,16);
                            int height = (int) (screenWidthPixels * scale);
                            ViewGroup.LayoutParams params = iv_intro.getLayoutParams();
                            params.height = height;
                            params.width = screenWidthPixels;
                            iv_intro.setImageBitmap(loadedImage);
                        }
                    });
            tv_intro.setText(introContent);
        }
    }

    // 创建ViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_item_intro, parent, false);
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(ViewHolder holder, int position){
        holder.bindData(imgUrl, introContent);
    }

    @Override
    public int getItemCount() {
        return 1;
    }
}